define
(
    ["require",
     "lib/knockout",
     "../Types"],
    function (require, ko, Types)
    {
        ko.bindingHandlers.applyClassBind = 
        {
            init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext)
            {                
                var bindingTerm = valueAccessor();
                var classNames = bindingTerm.classes;

                if (!Types.isArray(classNames))
                {
                    return;
                }

                var property = bindingTerm.property;

                // Create computed observable that will be executed whenever property changes
                ko.computed(function()
                {
                    var valueUnwrapped = ko.utils.unwrapObservable(property);

                    if (Types.isFunction(valueUnwrapped))
                    {
                        valueUnwrapped = valueUnwrapped();
                    }

                    for (var i = 0; i < classNames.length; i++)
                    {
                        var className = classNames[i];
                        if (valueUnwrapped)
                        {
                            $(element).addClass(className);
                        }
                        else
                        {
                            $(element).removeClass(className);
                        }
                    }
                });
            }
        };
    }
);